import Instance from "../Utils/Instance";

export default class Log {

    public logLevel: number = 1;
    public static get ins(): Log {
        return Instance.get(Log);
    }
    public log(logLevel: number = 1, ...message: any): void {
        if (logLevel >= this.logLevel) {
            for (let i = 0; i < message.length; i++) {
                const msg = message[i];
                if (typeof msg === `string`) {
                    const formattedMessage = this.formatMessage(msg, logLevel);
                    this.emit(formattedMessage, logLevel);
                } else {
                    console.log(msg);
                }
            }
        }
    }

    private formatMessage(message: string, logLevel: number): string {
        const timestamp = new Date().toLocaleString();
        return `[${timestamp}] [${this.getLogLevelString(logLevel)}] ${message}`;
    }

    private getLogLevelString(logLevel: number): string {
        switch (logLevel) {
            case 1:
                return "DEBUG";
            case 2:
                return "INFO";
            case 3:
                return "WARNING";
            case 4:
                return "ERROR";
            default:
                return "UNKNOWN";
        }
    }
    private levelcolor(logLevel: number): string {
        switch (logLevel) {
            case 1:
                return "black";
            case 2:
                return "green";
            case 3:
                return "red";
            case 4:
                return "red";
            default:
                return "blue";
        }
    }
    private emit(formattedMessage: string, logLevel: number): void {
        console.log(`%c${formattedMessage}`, `color:${this.levelcolor(logLevel)};font-size:10px`);
    }
}